package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.JsDeptService;
import com.pro.utils.FastDFSClientUtil;
import com.pro.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:JsDeptController
 * Package:com.pro.controller
 * Description: 后台部门Controller
 *
 * @Date:2022/1/19 下午 9:04
 * @Author: WYH
 */
@RestController
@RequestMapping("/jurisdiction")
public class JsDeptController {

    @Autowired(required = false)
    private JsDeptService jsDeptService;

    @Autowired
    private FastDFSClientUtil dfsClient;

    /**
     * @Author WYH
     * @Description //TODO 查看部门名称
     * @Date 下午 9:05 2022/1/19
     * @Param
     * @return  List<JsDept>
     **/
    @RequestMapping("/getJsDeptName")
    public JsonData getJsDeptName() {
        List<JsDept> jsDeptName = jsDeptService.getJsDeptName();
        return jsDeptName != null ? JsonData.buildSuccess(jsDeptName) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 查询后台用户信息
     * @Date 下午 9:18 2022/1/19
     * @Param account,trueName.deptName,registerDate,limit,page
     * @return  List<JsUser>
     **/
    @RequestMapping("/getJsUser")
    public JsonData getJsUser(String account, String trueName, String deptName, String registerDate,Integer limit,Integer page) {
        account = !("").equals(account) && account != null ? "%" + account + "%" : "%%";
        trueName = !("").equals(trueName) && trueName != null ? "%" + trueName + "%" : "%%";
        deptName = !("").equals(deptName) && deptName != null ? "%" + deptName + "%" : "%%";
        registerDate = !("").equals(registerDate) && registerDate != null ? "%" + registerDate + "%" : "%%";
        List<JsUser> jsUsers = jsDeptService.getJsUser(account, trueName, deptName, registerDate, limit, page);
        Integer integer = jsDeptService.countJsUser(account, trueName, deptName, registerDate);
        return jsUsers != null ? JsonData.buildSuccess(jsUsers,integer) : JsonData.buildError("数据格式异常");
    }

    JsClosure jsClosure = new JsClosure();
    /**
     * @Author WYH
     * @Description //TODO 更新账号状态
     * @Date 下午 7:20 2022/1/20
     * @Param jsUser
     * @return  false | true
     **/
    @RequestMapping("/updateStatus")
    public JsonData updateStatus(JsUser jsUser){
        Boolean updateStatus = jsDeptService.updateStatus(jsUser);
        Integer status = jsUser.getStatus();
        Integer enabled = jsUser.getEnabled();
        Boolean aBoolean1 = false;
        Boolean look = false;

        if(status==1&&enabled==1){
            aBoolean1 = jsDeptService.insertUnseal(jsUser.getUserId(),jsUser.getType(),jsUser.getUnsealDate());
            Boolean aBoolean2 = jsDeptService.updateUserEndDate(jsClosure.getId(),jsUser.getUserId(), jsUser.getUnsealDate());
            if(updateStatus&&aBoolean1&&aBoolean2!=false){
                look = true;
            }
        }else{
            jsClosure.setUserId(jsUser.getUserId());
            jsClosure.setType(3);
            jsClosure.setRemarks(jsUser.getRemark());
            jsClosure.setStartDatee(jsUser.getUnsealDate());
            jsClosure.setEndDatee(jsUser.getEndDate());
            jsClosure.setDays(36524);
            jsClosure.setRemarks("紧急封停");
            Boolean aBoolean2 = jsDeptService.addUserClosure(jsClosure);
            if(updateStatus&&aBoolean2!=false){
                look=true;
            }
        }

        return look != false ? JsonData.buildSuccess(updateStatus) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 查询用户详情
     * @Date 下午 7:35 2022/1/20
     * @Param userId
     * @return  JsUser
     **/
    @RequestMapping("/getJsUserById")
    public JsonData getJsUserById(Integer userId){
        JsUser jsUserById = jsDeptService.getJsUserById(userId);
        return jsUserById != null ? JsonData.buildSuccess(jsUserById) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 高级检索
     * @Date 下午 8:40 2022/1/21
     * @Param email,phone,idCard,status,sex,enabled,limit,page
     * @return JsonData
     **/
    @RequestMapping("/getSearchJsUser")
    public JsonData getSearchJsUser(String email,String phone,String idCard,String status,String sex,String enabled,Integer limit,Integer page){
        email = !("").equals(email) && email !=null?"%"+email+"%":"%%";
        phone = !("").equals(phone) && phone !=null?"%"+phone+"%":"%%";
        idCard = !("").equals(idCard) && idCard !=null?"%"+idCard+"%":"%%";
        status = !("").equals(status) && status !=null?"%"+status+"%":"%%";
        sex = !("").equals(sex) && sex !=null?"%"+sex+"%":"%%";
        enabled = !("").equals(enabled) && enabled !=null?"%"+enabled+"%":"%%";
        List<JsUser> searchJsUser = jsDeptService.getSearchJsUser(email, phone, idCard, status, sex, enabled, limit, page);
        Integer integer = jsDeptService.countSearchJsUser(email, phone, idCard, status, sex, enabled);
        return searchJsUser != null ? JsonData.buildSuccess(searchJsUser,integer) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 权限
     * @Date 下午 9:05 2022/1/21
     * @Param userId
     * @return  JsonData
     **/
    @RequestMapping("/getUserSettings")
    public JsonData getUserSettings(Integer userId){
        List<JsUser> userSettings = jsDeptService.getUserSettings(userId);
        return userSettings != null ? JsonData.buildSuccess(userSettings) : JsonData.buildError("数据格式异常");
    }



    /**
     * @Author WYH
     * @Description //TODO 删除后台权限
     * @Date 下午 8:51 2022/1/24
     * @Param userId
     * @return  true | false
     **/
    @RequestMapping("/delUserStatus")
    public JsonData delUserStatus(Integer userId){
        Boolean delUserStatus = jsDeptService.delUserStatus(userId);
        return delUserStatus != false ? JsonData.buildSuccess(delUserStatus) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 对后台用户账户进行编辑
     * @Date 下午 9:39 2022/1/24
     * @Param jsUser
     * @return JsDept
     **/
    @RequestMapping("/updateUser")
    public JsonData updateUser(JsUser jsUser){
        System.err.println(jsUser);
        Boolean updateUser = jsDeptService.updateUser(jsUser);
        return updateUser != false ? JsonData.buildSuccess(updateUser) : JsonData.buildError("数据格式异常");
    }

    /**
     * @Author WYH
     * @Description //TODO 上传图片
     * @Date 下午 4:25 2022/1/25
     * @Param
     * @return
     **/
    @RequestMapping("/uploadFile")
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String resAccessUrl =null;
        String fileUrl = null;
        try {
            fileUrl = dfsClient.uploadFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //路径
        resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }

    /**
     * @Author WYH
     * @Description //TODO 封停
     * @Date 下午 6:11 2022/1/25
     * @Param jsClosure
     * @return  JsDept
     **/
    @RequestMapping("/addUserClosure")
    public JsonData addUserClosure(JsClosure jsClosure) {
        Boolean updateUserStatus = jsDeptService.updateUserStatus(jsClosure.getUserId(), jsClosure.getStatus(), jsClosure.getEnabled());
        Boolean addUserClosure = jsDeptService.addUserClosure(jsClosure);
        Boolean look = false;
        if(updateUserStatus!=false&&addUserClosure!=false){
            look = true;
        }
        return look != false ? JsonData.buildSuccess(look) : JsonData.buildError("服务端错误");
    }
    
    /**
     * @Author WYH
     * @Description //TODO 添加后台人员
     * @Date 下午 8:52 2022/1/27
     * @Param jsUser
     * @return  JsDept
     **/
    @RequestMapping("/addBackgroundUser")
    public JsonData addBackgroundUser(JsUser jsUser){
        //获取的是本地的IP地址
        InetAddress address = null;
        String hostAddress= "";
        try{
            address = InetAddress.getLocalHost();
            hostAddress = address.getHostAddress();
        }catch (Exception e){
            e.printStackTrace();
        }
        jsUser.setRegisterIp(hostAddress);
        String password = MD5Util.md5(jsUser.getPassword());
        jsUser.setPassword(password);
        Boolean aBoolean = jsDeptService.addBackgroundUser(jsUser);

        Boolean aBoolean1 = addUserStatus(jsUser);

        Boolean b = false;
        if(aBoolean&&aBoolean1!=false){
            b=true;
        }
        return b != false ? JsonData.buildSuccess(b): JsonData.buildError("服务端错误");
    }

    /**
     * @Author WYH
     * @Description //TODO 添加后台权限
     * @Date 下午 8:51 2022/1/24
     * @Param jsUser
     * @return  true | false
     **/
    @RequestMapping("/addUserStatus")
    public Boolean addUserStatus(JsUser jsUser){
        Integer[] authorityIdd = jsUser.getAuthorityIdd();
        List<JsUser> list = new ArrayList();
        for (int i = 0; i <authorityIdd.length ; i++) {
            JsUser jsUser1 = new JsUser();
            jsUser1.setUserId(jsUser.getUserId());
            jsUser1.setAuthorityId(authorityIdd[i]);
            jsUser1.setType(jsUser.getType());
            jsUser1.setCreateId(jsUser.getCreateId());
            jsUser1.setCreateDate(jsUser.getCreateDate());
            list.add(jsUser1);
        }
        Boolean aBoolean1 = jsDeptService.addUserStatus(list);
        return aBoolean1;
    }

    /**
     * @Author WYH
     * @Description //TODO 后台人员解封日志
     * @Date 下午 9:07 2022/2/7
     * @Param account,deptName,unsealDate,limit,page
     * @return  JsonData
     **/
    @RequestMapping("/getPeopleUnsealing")
    public JsonData getPeopleUnsealing(String account, String deptName, String unsealDate, Integer limit, Integer page){
        account = !("").equals(account) && account !=null?"%"+account+"%":"%%";
        deptName = !("").equals(deptName) && deptName !=null?"%"+deptName+"%":"%%";
        unsealDate = !("").equals(unsealDate) && unsealDate !=null?"%"+unsealDate+"%":"%%";
        List<JsUnseal> peopleUnsealing = jsDeptService.getPeopleUnsealing(account, deptName, unsealDate, limit, page);
        Integer integer = jsDeptService.countGetPeopleUnsealing(account, deptName, unsealDate);
        return peopleUnsealing != null ? JsonData.buildSuccess(peopleUnsealing,integer) : JsonData.buildError("数据格式异常");
    }
}